/*
 *  Copyright 2014-2015 snakerflow.com
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *     http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *
 */
package com.base.shiro.web;

import java.util.List;
import java.util.Map;

import com.base.common.utils.map.MapUtil;
import com.base.shiro.entity.ResModel;
import com.base.shiro.entity.RoleModel;
import com.base.shiro.web.validator.RoleValidator;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 * RoleController
 * @author yuqs
 * @since 0.1
 */
public class RoleController extends Controller {
	
	
	public void index() {		
		
		render("roleList.jsp");
	}
	public void roleList(){	
//		int start=(getParaToInt("start")!=0)?getParaToInt("start"):1;
		int pageNumber=(getParaToInt("pageIndex")!=0)?getParaToInt("pageIndex")+1:1;		
		int pageSize=(getParaToInt("limit")!=0)?getParaToInt("limit"):10;		
		Page<RoleModel> roleList= RoleModel.dao.paginate(pageNumber,pageSize,getPara("name"));
		Map<String, Object> map=MapUtil.toModel("rows", roleList);
//		map.put("rows",goodsModelList.getList() );
		map.put("results",roleList.getTotalRow());
		map.put("hasError", false);		
		renderJson(map);
		
	}
	public void add() {
		RoleModel role =new RoleModel();
		setAttr("Role", role);
		render("roleAdd.jsp");
	}
	public void addAuthc() {
		setAttr("authorities", ResModel.dao.getAllAuthoritys());
		render("roleAdd.jsp");
	}
	
	public void edit() {
		setAttr("role", RoleModel.dao.findById(getParaToInt()));
		List<ResModel> authorities = ResModel.dao.getAllAuthoritys();
		List<ResModel> auths = RoleModel.dao.getAuthorities(getParaToInt());
		for(ResModel auth : authorities) {
			for(ResModel sels : auths) {
				if(auth.getInt("id").intValue() == sels.getInt("id").intValue())
				{
					auth.put("selected", 1);
				}
				if(auth.get("selected") == null)
				{
					auth.put("selected", 0);
				}
			}
		}
		setAttr("authorities", authorities);
		render("roleEdit.jsp");
	}
	
	public void view() {
		setAttr("role", RoleModel.dao.findById(getParaToInt()));
		setAttr("authorities", RoleModel.dao.getAuthorities(getParaToInt()));
		render("roleView.jsp");
	}
	
	@Before({RoleValidator.class, Tx.class})
	public void save() {
		Integer[] orderIndexs = getParaValuesToInt("orderIndexs");
		RoleModel model = getModel(RoleModel.class);
		model.save();
		if(orderIndexs != null) {
			for(Integer orderIndex : orderIndexs) {
				RoleModel.dao.insertCascade(model.getInt("id"), orderIndex);
			}
		}
		redirect("/ms/shiro/role");
	}
	
	@Before({RoleValidator.class, Tx.class})
	public void update() {
		Integer[] orderIndexs = getParaValuesToInt("orderIndexs");
		RoleModel model = getModel(RoleModel.class);
		model.update();
		RoleModel.dao.deleteCascade(model.getInt("id"));
		if(orderIndexs != null) {
			for(Integer orderIndex : orderIndexs) {
				RoleModel.dao.insertCascade(model.getInt("id"), orderIndex);
			}
		}
		redirect("/ms/shiro/role");
	}
	
	@Before(Tx.class)
	public void delete() {
		RoleModel.dao.deleteCascade(getParaToInt());
		RoleModel.dao.deleteById(getParaToInt());
		redirect("/ms/shiro/role");
	}
}


